<?php
// +----------------------------------------------------------------------
// | 07FLYSAAS [基于ThinkPHP5.0开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2025-2035 http://www.07fly.xyz
// +----------------------------------------------------------------------
// | Professional because of focus  Persevering because of happiness
// +----------------------------------------------------------------------
// | Author: 开发人生 <goodkfrs@qq.com>
// +----------------------------------------------------------------------

namespace app\plat\logic;

/**
 * 菜单逻辑
 */
class TenMenu extends PlatBase
{

    // 面包屑
    public static $crumbs = [];

    // 菜单Select结构
    public static $menuSelect = [];

    /**
     * 左侧菜单转视图
     */
    public function menuToView($menu_list = [], $child = 'child', $level = 0)
    {

        $menu_view = '';

        //遍历菜单列表
        foreach ($menu_list as $menu_info) {

            if (!empty($menu_info[$child]) && $menu_info['visible'] == 1) {

                $icon = empty($menu_info['icon']) ? 'fa-dot-circle-o' : $menu_info['icon'];
                $level_classname = '';
                if ($level == 0) {
                    $level_classname = "nav-second-level";
                } else if ($level == 1) {
                    $level_classname = "nav-third-level";
                }
                $menu_view .= '
                                        <li> <a href="#"> <i class="fa ' . $icon . '"></i> <span class="nav-label">' . lang($menu_info['name']) . '</span> <span class="fa arrow"></span> </a>
                                            <ul class="nav ' . $level_classname . '">
                                               ' . $this->menuToView($menu_info[$child], $child, $level + 1) . '
                                            </ul>
                                        </li>';
            } else {

                $icon = empty($menu_info['icon']) ? 'fa-circle-o' : $menu_info['icon'];

                //defined('BIND_MODULE') && $menu_info['url'] =$menu_info['module'].'/'.$menu_info['url'];//解决模块间的地址

                $menu_info['url'] = $menu_info['module'] . '/' . $menu_info['url'];//解决模块间的地址

                $url = url($menu_info['url']);

                if ($menu_info['visible'] == 1) {
                    //$menu_view .= "<li menu_id='".$menu_info['id']."'><a href='$url'><i class='fa $icon'></i> <span>".$menu_info['name']."</span></a></li>";
                    $menu_view .= '<li> <a class="J_menuItem" href="' . $url . '"> <i class="fa ' . $icon . '"></i> <span class="nav-label">' .  lang($menu_info['name'])  . '</span> </a> </li>';
                }
            }
        }

        return $menu_view;
    }

    /**
     * 菜单转Checkbox,用于菜单授权勾选
     *
     * @param array $menu_list 菜单列表
     * @param string $child 子菜单字段名
     * @param array $rules_array 菜单选中值
     * @return string
     */
    public function menuTreeCheckboxView($menu_list = [], $child = 'child', $rules_array = [])
    {
        $menu_view = '';
        $rules_array = is_array($rules_array) ? $rules_array : explode(',', $rules_array);

        //遍历菜单列表
        foreach ($menu_list as $menu_info) {
            //$icon = empty($menu_info['icon']) ? 'fa-dot-circle-o' : $menu_info['icon'];
            $icon = '';
            $checkbox_select = in_array($menu_info['id'], $rules_array) ? "checked='checked'" : '';
            if (!empty($menu_info[$child])) {
                $menu_view .= '<li class="has_child">';
                $menu_view .= "<span><i class='icon-plus-sign'></i></span><input class='rules_all' type='checkbox' name='menu_ids[]' value='" . $menu_info['id'] . "' $checkbox_select ><i class='fa $icon'></i> " . $menu_info['name'] . "";
                $menu_view .= "<ul style='display: none;'>";
                $menu_view .= $this->menuTreeCheckboxView($menu_info[$child], $child, $rules_array);
                $menu_view .= "</ul>";
                $menu_view .= "</li>";
            } else {
                $menu_view .= "<li>";
                $menu_view .= "<span><i></i></span><input type='checkbox' name='menu_ids[]' value='" . $menu_info['id'] . "'  $checkbox_select ><i class='fa $icon'></i> " . $menu_info['name'] . "";
                $menu_view .= "</li>";
                //$menu_view .= "<a class='auth-head'><input type='checkbox' name='rules[]' value='" . $menu_info['id'] . "'  $checkbox_select ><i class='fa $icon'></i> " . $menu_info['name'] . "&nbsp;&nbsp;</a>";
            }
        }
        return $menu_view;
    }

    /**
     * 获取自己及父菜单列表
     */
    public function getParentMenuList($menu_id = 0)
    {
        $menu_info = $this->getTenMenuInfo(['id' => $menu_id]);
        !empty($menu_info['pid']) && $this->getParentMenuList($menu_info['pid']);
        self::$crumbs [] = $menu_info;
    }

    /**
     * 获取面包屑
     */
    public function getCrumbsView()
    {
        $map['url'] = URL;
        $map['module'] = MODULE_NAME;
        $menu_info = $this->getTenMenuInfo($map);

        // 获取自己及父菜单列表
        !empty($menu_info['id']) && $this->getParentMenuList($menu_info['id']);

        $crumbs_view = '<div class="row white-bg" style="padding: 10px 5px 10px 20px;font-size: 13px;"><div class="col-sm-12">';

        $crumbs_view .= "<ol class='breadcrumb'>";

        foreach (self::$crumbs as $menu_info) {

            $icon = empty($menu_info['icon']) ? 'fa-circle-o' : $menu_info['icon'];

            $crumbs_view .= "<li><a><i class='fa $icon'></i> " . $menu_info['name'] . "</a></li>";
        }

        $crumbs_view .= "</ol>";
        $crumbs_view .= "</div></div>";

        return $crumbs_view;
    }

    /**
     * 获取默认页面标题
     */
    public function getDefaultTitle()
    {
        $name = $this->modelTenMenu->getValue(['module' => MODULE_NAME, 'url' => URL], 'name');
        if (!empty($name)) {
            $name = lang($name);
        }
        return $name;
    }

    /**
     * 获取菜单信息
     */
    public function getTenMenuInfo($where = [], $field = true)
    {
        $this->modelTenMenu->setIgnoreTenantCheck();
        $info = $this->modelTenMenu->getInfo($where, $field);
        if (!empty($info)) {
            $info['name'] = lang($info['name']);
        }
        return $info;
    }
}